package register;

import java.io.IOException;
import java.io.PrintWriter;

import com.common.Mail;
import com.common.RegisterTool;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import javax.mail.MessagingException;
import javax.servlet.http.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class findPassword extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String UserName;
    private String Email;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String checkUser() throws IOException {

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        RegisterTool tool = (RegisterTool) context.getBean("registerTool");

        String sql = "select * from Account where username=?";
        boolean flag = tool.checkUser(sql, UserName);
        if (flag){
            writer.write("true");
        }
        writer.close();
        return null;
    }

    public String getEmailNum() throws IOException {

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        HttpSession httpSession = ServletActionContext.getRequest().getSession();

        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        RegisterTool tool = (RegisterTool) context.getBean("registerTool");
        String sql = "select Email from Account where username=?";
        String email = tool.getEmailNum(sql, UserName);

        if (email != null){
            writer.write("true");
            httpSession.setAttribute("email", email);
        }
        writer.close();
        return null;
    }

    public String changePassword() throws IOException {

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        RegisterTool tool = (RegisterTool) context.getBean("registerTool");

        String sql = "update Account set passwold=? where Email=?";
        int columnNum = tool.changePassword(sql, password, Email);
        if (columnNum > 0) {
            writer.write("true");
        }
        writer.close();
        return null;
    }

    public String sendCode() throws IOException, MessagingException {

        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        HttpSession httpSession = ServletActionContext.getRequest().getSession();
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");

        int codeLength = 6;
        StringBuffer buffer = new StringBuffer();
        int[] random = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = 0; i < codeLength; i++) {
            int index = (int) (Math.random() * 10);
            buffer.append(random[index]);
        }
        String code = buffer.toString();
        httpSession.setAttribute("code", code);
        String content = ""
                + "您好！感谢您使用长江上游珍稀特有鱼类信息管理系统。以下是您的验证码，切勿告知他人，谢谢！：\n"
                + "验证码：" + code + "\n" + "网址：http://fish.yfi.ac.cn\n"
                + "如有任何疑问或需要帮助，欢迎随时联系我们。\n" + "再次感谢您使用本系统，谢谢！\n" + "长江水产研究所";

        Mail mail = (Mail) context.getBean("mail");
        mail.sendMail("2247072438@qq.com", Email, "验证码", content);

        System.out.println(code);
        writer.write("true");
        writer.close();
        return null;
    }

}
